package com.edgclub.edg.utils.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;

public class JsonUtil {

	/**
	 * Mode转Json字符串
	 * 
	 * @param <T>
	 * @param t
	 * @return
	 */
	public static synchronized <T> String getJsonStr(T t) {
		Gson gson = new Gson();
		return gson.toJson(t);
	}

	/**
	 * Json转对象，可以是mode,也可以是mode集合
	 * 
	 * @param <T>
	 * @param gsonStr
	 * @param type
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static synchronized <T> T getMode(String gsonStr, Type type) {
		Gson gson = new Gson();
		return (T) gson.fromJson(gsonStr, type);
	}

	@SuppressWarnings("unchecked")
	public static synchronized <T> T getMode2(String gsonStr, Type type) {
		Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
				.create();

		return (T) gson.fromJson(gsonStr, type);
	}

}
